// $Id: WorkflowHistoryView.java 85 2012-02-25 15:01:58Z huijzer@gmail.com $
package lime.workflow.view;

import java.util.List;

import lime.application.provider.DefaultStructuredContentProvider;
import lime.application.provider.DefaultTableLabelProvider;
import lime.model.Workitem;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

public class WorkflowHistoryView extends AbstractWorkitemListenerView {

	public static final String ID = "lime.workflow.view.history";
	
	// Widgets
	private TableViewer viewer;
	
	@Override
	public void createPartControl(Composite parent) {
	
		viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);
	
		TableViewerColumn sequenceNumberColumn = new TableViewerColumn(viewer, SWT.NONE);
		sequenceNumberColumn.getColumn().setText("#");
		TableViewerColumn creationDateColumn = new TableViewerColumn(viewer, SWT.NONE);
		creationDateColumn.getColumn().setText("Creation Date");
		TableViewerColumn activityNameColumn = new TableViewerColumn(viewer, SWT.NONE);
		activityNameColumn.getColumn().setText("Activity Name");
		TableViewerColumn runtimeStateColumn = new TableViewerColumn(viewer, SWT.NONE);
		runtimeStateColumn.getColumn().setText("Runtime State");
		
		viewer.setContentProvider(new HistoryContentProvider());
		viewer.setLabelProvider(new HistoryLabelProvider());
		
		// Make sure the columns take up as much space as needed, but no more.
		for (TableColumn column : viewer.getTable().getColumns()) {
			column.pack();
		}
	
		getSite().setSelectionProvider(viewer);
	}

	@Override
	public void setFocus() {
		viewer.getTable().setFocus();
	}

	private class HistoryContentProvider extends DefaultStructuredContentProvider {
		@SuppressWarnings("unchecked")
		@Override
		public Object[] getElements(Object element) {
			List<Workitem> workitems = (List<Workitem>) element;
			return workitems.toArray();
		}
	}
	
	private class HistoryLabelProvider extends DefaultTableLabelProvider {
	
		@Override
		public String getColumnText(Object element, int columnIndex) {
			Workitem workitem = (Workitem) element;
			String text = "";
			
			switch (columnIndex) {
				case 0:
					text = workitem.getSequenceNumber();
					break;
				case 1:
					text = workitem.getWorkitemStart();
					break;
				case 2:
					text = workitem.getActivityName();
					break;
				case 3:
					text = workitem.getRuntimeState();
					break;
			default:
					break;
			}
			
			return text;
		}
	}
	
	@Override
	protected void update(final Workitem workitem) {
	
		final Thread myThread = new Thread("MyThread") {
			public void run() {
				String workflowId = workitem.getWorkflowId();
				final List<Workitem> workitems = getWorkflowService().getWorkflowHistory(workflowId);
				getSite().getShell().getDisplay().asyncExec(new Runnable() {
					public void run () {
						setContentDescription(workitem.getProcessName() + " :: " + workitem.getActivityName());
						viewer.setInput(workitems);
						// Make sure the columns take up as much space as
						// needed, but no more.
						for (TableColumn column : viewer.getTable().getColumns()) {
							column.pack();
						}
						viewer.refresh();
					}
				});
			}
		};
		
		myThread.start();
	}
	
}